<?php

/*
 * manage actions about chat
 */

class ChatController extends Controller {

    public function init() {
	if (user()->isGuest) {
	    $this->redirect(SITE_NAME_URL);
	}
    }

    /**
     * send message 
     * param: idUser from ajax $.post
     */
    public function actionSend() {

	//add new msg
	if (isset($_POST['idUser']) && isset($_POST['content']) && isset($_POST['userName'])) {
	    $modelChat = new Chat();

	    //get recent msg
	    if (isset($_POST['timeLastMsg']) && $_POST['timeLastMsg'] != null) {
		$timeLastMsg = $_POST['timeLastMsg'] + 1;

		$aMsgs = $modelChat->findAll(array(
		    'condition' => "((user_send = " . user()->id . " and user_receive = " . $_POST['idUser'] . ") or (user_send = " . $_POST['idUser'] . " and user_receive = " . user()->id . ")) and time_created between " . $timeLastMsg . " and " . time()
		));
		foreach ($aMsgs as $message) {
		    echo '<li time-created="' . $message->time_created . '">' . $message->username_send . ': ' . $message->message . '</li>';
		}
	    }
	    $modelChat->user_send = user()->id;
	    $modelChat->user_receive = $_POST['idUser'];
	    $modelChat->message = $_POST['content'];
	    $modelChat->username_send = user()->getModel()->username;
	    $modelChat->username_receive = $_POST['userName'];
	    $modelChat->time_created = time();
	    $modelChat->save();

	    echo "<li time-created='" . time() . "'>" . user()->getModel()->username . ": " . $_POST['content'] . "</li>";
	}
    }

    /**
     * get message
     */
    public function actionGetMsg() {
	if (isset($_POST['timeLastMsg']) && $_POST['timeLastMsg'] != null && isset($_POST['idUser'])) {
	    $modelChat = new Chat();
	    $timeLastMsg = $_POST['timeLastMsg'] + 1;
	    $aMsgs = $modelChat->findAll(array(
		'condition' => "((user_send = " . user()->id . " and user_receive = " . $_POST['idUser'] . ") or (user_send = " . $_POST['idUser'] . " and user_receive = " . user()->id . ")) and time_created between " . $timeLastMsg . " and " . time()
	    ));
	    foreach ($aMsgs as $message) {
		echo '<li time-created="' . $message->time_created . '">' . $message->username_send . ': ' . $message->message . '</li>';
	    }
	}
    }

    /**
     * check if user have received a message from another
     */
    public function actionCheck() {
	$limitTime = time() - 15; //get message in 30s
	$aUserChat = Yii::app()->db->createCommand("
	    SELECT * from 
	    (SELECT * from {{chats}} order by chat_id desc limit 0, 50) as chat
	     where user_receive = " . user()->id . " and time_created BETWEEN " . $limitTime . " and " . time() . "
	    GROUP BY username_send
	    ORDER BY chat_id DESC
	    limit 4
	    ")->queryAll();
	echo json_encode($aUserChat);
    }

    /**
     * check online
     */
    public function actionCheckOnline() {
	if (isset($_POST['idUser'])) {
	    $aLastOnline = Yii::app()->db->createCommand('SELECT last_online FROM {{users}} WHERE user_id = ' . $_POST['idUser'])
		    ->queryRow();
	    if (count($aLastOnline)) {
		//In 5 minutes, if user sended request to server, confirm that user is online
		$timeUserOnline = time() - $aLastOnline['last_online'];
		if ($timeUserOnline < 30) {
		    //if user is online
		    echo 'true';
		} else {
		    //if user is offline
		    echo 'false';
		}
	    }
	}
    }

}

